package gitapp.oschina.net.my_osc.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.TextView;

import java.util.ArrayList;

import gitapp.oschina.net.my_osc.R;
import gitapp.oschina.net.my_osc.widget.PagerSlidingTabStrip;

/**
 * @author Tristan-Hou
 */
public class ViewPageFragmentAdapter extends FragmentPagerAdapter
        implements ViewPager.OnPageChangeListener {

    protected PagerSlidingTabStrip mPagerStrip;
    private final Context mContext;
    private final ViewPager mViewPager;
    private final ArrayList<ViewPageInfo> viewPageList = new ArrayList<ViewPageInfo>();




    public ViewPageFragmentAdapter(FragmentManager fm,
                                   PagerSlidingTabStrip pageStrip,
                                   ViewPager viewPager) {
        super(fm);
        this.mPagerStrip = pageStrip;
        this.mContext = viewPager.getContext();
        this.mViewPager = viewPager;
        this.mViewPager.setAdapter(this);
        this.mViewPager.setOnPageChangeListener(this);
        mPagerStrip.setViewPager(this.mViewPager);
    }

    public void addTab(String title,  String tag,Class<?> cls, Bundle bundle){
        ViewPageInfo viewPageInfo = new ViewPageInfo(title,tag,cls,bundle);
        viewPageList.add(viewPageInfo);
    }

    /**
     * Return the Fragment associated with a specified position.
     *
     * @param position
     */
    @Override
    public Fragment getItem(int position) {
        ViewPageInfo info = viewPageList.get(position);
        return Fragment.instantiate(mContext, info.cls.getName(), info.args);
    }

    /**
     * Return the number of views available.
     */
    @Override
    public int getCount() {
        return viewPageList.size();
    }

    /**
     * This method should be called by the application if the data backing this adapter has changed
     * and associated views should update.
     */
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        for (ViewPageInfo viewPageInfo : viewPageList){
            TextView v = (TextView) LayoutInflater.from(mContext).inflate(R.layout.sliding_tab_item, null);
            v.setText(viewPageInfo.title);
            mPagerStrip.addTab(v);
        }


    }

    /**
     * This method will be invoked when the current page is scrolled, either as part
     * of a programmatically initiated smooth scroll or a user initiated touch scroll.
     *
     * @param position             Position index of the first page currently being displayed.
     *                             Page position+1 will be visible if positionOffset is nonzero.
     * @param positionOffset       Value from [0, 1) indicating the offset from the page at position.
     * @param positionOffsetPixels Value in pixels indicating the offset from position.
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * This method will be invoked when a new page becomes selected. Animation is not
     * necessarily complete.
     *
     * @param position Position index of the new selected page.
     */
    @Override
    public void onPageSelected(int position) {

    }

    /**
     * Called when the scroll state changes. Useful for discovering when the user
     * begins dragging, when the pager is automatically settling to the current page,
     * or when it is fully stopped/idle.
     *
     * @param state The new scroll state.
     * @see ViewPager#SCROLL_STATE_IDLE
     * @see ViewPager#SCROLL_STATE_DRAGGING
     * @see ViewPager#SCROLL_STATE_SETTLING
     */
    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
